package com.iqiyi.pps.epg.web.timer;

import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.model.carousel.AutoCarouselEpisodeRule;
import com.iqiyi.pps.epg.core.service.carousel.CarouselService;
import com.iqiyi.pps.epg.core.service.carousel.ICarouselAsyncService;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by karkarrotxu on 2016/1/27.
 */
public class AutomationAppendTrigger {
    private static Logger logger = LoggerFactory.getLogger(AutomationAppendTrigger.class);
    @Autowired
    private ICarouselAsyncService carouselAsyncService;
    @Autowired
    private CarouselService carouselService;

    public void task() {
        logger.info("[AutomationAppendTrigger] start trigger");
        long startTime = System.currentTimeMillis();
        //, int dayAfter
        int createDay = Configuration.getInt("automation.createDay");
        //int dayAfter = createDay + 1;
        long checkEndTime = DateTimeUtil.getDayEndTime(startTime + DateTimeUtil.DAY_TIME * (createDay - 1));
        List<AutoCarouselEpisodeRule> resultList = carouselService.getAutomationToUpdate();
        for (AutoCarouselEpisodeRule temp : resultList) {
            if (temp.getExecuteTime().getTime() < checkEndTime) {
//                if (temp.getExecuteTime().getTime() < startTime) {
//                    logger.info("[AutomationAppendTrigger] executeTime < startTime channelId={},ruleId={} , execTime ={}", temp.getFatherId(), temp.getId(), temp.getExecuteTime().getTime());
//                    continue;
//                }
                carouselAsyncService.addAutomationAppendTask(temp.getFatherId(), checkEndTime, temp.getType());
        }
        }
        carouselAsyncService.triggerAppendOnce();
        logger.info("[AutomationAppendTrigger] end trigger . time:" + (System.currentTimeMillis() - startTime));
    }
}
